package manage.modules.action.service.impl;

import java.util.List;

import manage.commons.utils.Pager;
import manage.commons.utils.TableNameUtil;
import manage.modules.BaseService;
import manage.modules.action.model.ActionUri;
import manage.modules.action.service.ActionUriService;
import manage.modules.rbac.model.ActionOfMenu;
import manage.modules.rbac.model.ActionOfRole;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

public class ActionUriServiceImpl implements ActionUriService {
	private static final String ACTION_TABLE_PRIMARY = "actionId";
	private static final String ACTION_CACHE_KEY = "allAction";
	private BaseService baseService;

	@Override
	public ActionUri select(Integer actionId) {
		return baseService.select(ACTION_TABLE_PRIMARY, actionId, getClazz());
	}

	@Override
	public ActionUri select(String uri) {
		return baseService.select("actionUri", uri, getClazz());
	}

	@Override
	public int insert(ActionUri action) {
		int count = baseService.insert(action);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public int update(ActionUri action) {
		int count = baseService.updateFields(ACTION_TABLE_PRIMARY,
				action.getActionId(), action);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public int delete(Integer actionId) {
		int count = baseService.delete(ACTION_TABLE_PRIMARY, actionId,
				getClazz());
		baseService.delete(ACTION_TABLE_PRIMARY, actionId, ActionOfRole.class);
		baseService.delete(ACTION_TABLE_PRIMARY, actionId, ActionOfMenu.class);
		clearCacheByCount(count);
		return count;
	}

	@Override
	public List<ActionUri> listAll() {
		List<ActionUri> allAction = baseService.getListByCache(
				ACTION_CACHE_KEY, getClazz());
		if (CollectionUtils.isEmpty(allAction)) {
			allAction = baseService.listAll(getClazz());
			if (CollectionUtils.isNotEmpty(allAction)) {
				baseService.setCache(ACTION_CACHE_KEY, allAction);
			}
		}
		return allAction;
	}

	@Override
	public boolean exists(Integer actionId) {
		return baseService.exists(ACTION_TABLE_PRIMARY, actionId, getClazz());
	}

	@Override
	public boolean exists(String actionUri) {
		return baseService.exists("actionUri", actionUri, getClazz());
	}

	@Override
	public Long getCountOfAll() {
		return baseService.countOfAll(getClazz());
	}

	@Override
	public void query(ActionUri action, Pager<ActionUri> pager) {
		StringBuilder querySql = new StringBuilder(1024);
		querySql.append("select * from "
				+ TableNameUtil.getTableName(getClazz()) + " where 1=1");
		if (action != null) {
			if (StringUtils.isNotBlank(action.getActionName())) {
				querySql.append(" and actionName like '%"
						+ action.getActionName() + "%'");
			}
			if (StringUtils.isNotBlank(action.getActionUri())) {
				querySql.append(" and actionUri like '%"
						+ action.getActionUri() + "%'");
			}
			if (StringUtils.isNotBlank(action.getNote())) {
				querySql.append(" and note like '%" + action.getNote() + "%'");
			}
			pager.setRowCount(baseService.getJdbcOperations().queryForLong(
					baseService.getCountSql(querySql.toString())));
			querySql.append(" limit ?,?");
			ParameterizedRowMapper<ActionUri> rowMapper = ParameterizedBeanPropertyRowMapper
					.newInstance(getClazz());
			pager.setList(baseService.getJdbcOperations().query(
					querySql.toString(), rowMapper, pager.getRowOffset(),
					pager.getPageSize()));
		}
	}

	private void clearCacheByCount(int count) {
		if (count > 0) {
			baseService.clearCache(ACTION_CACHE_KEY);
		}
	}

	public Class<ActionUri> getClazz() {
		return ActionUri.class;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

}
